delphi tstringlist使用教程 |
您所在的位置:网站首页 › delphi tstrings › delphi tstringlist使用教程 |
delphi tstringlist使用教程
精选
转载
jyxy521 2013-08-28 10:31:06 文章标签 delphi tstringlist使用 文章分类 后端开发 //TStringList 常用方法与属性:varList: TStringList;i: Integer;beginList := TStringList.Create;List.Add('Strings1'); {添加}List.Add('Strings2');List.Exchange(0,1); {置换}List.Insert(0,'Strings3'); {插入}i := List.IndexOf('Strings1'); {第一次出现的位置}List.Sort; {排序}List.Sorted := True; {指定排序}List.Count; {总数}List.Text; {文本集合}List.Delete(0); {删除, 0是第一个数据}List.LoadFromFile('c:\tmp.txt');{打开}List.SaveToFile('c:\tmp.txt'); {保存}List.Clear; {清空}List.Free; {释放}end;//读入字符串varList: TStringList;beginList := TStringList.Create;List.CommaText := 'aaa,bbb,ccc,ddd';//相当于: List.Text := 'aaa' + #13#10 + 'bbb' + #13#10' + 'ccc' + '#13#10' + 'ddd';ShowMessage(IntToStr(List.Count)); //4ShowMessage(List[0]); //aaaList.Free;end;//置换分隔符varList: TStringList;beginList := TStringList.Create;List.Delimiter := '|';List.DelimitedText := 'aaa|bbb|ccc|ddd';ShowMessage(IntToStr(List.Count)); //4ShowMessage(List[0]); //aaaList.Free;end;//类似的哈希表操作法varList: TStringList;beginList := TStringList.Create;List.Add('aaa=111');List.Add('bbb=222');List.Add('ccc=333');List.Add('ddd=444');ShowMessage(List.Names[1]); //bbbShowMessage(List.ValueFromIndex[1]); //222ShowMessage(List.Values['bbb']); //222//ValueFromIndex 可以赋值:List.ValueFromIndex[1] := '2';ShowMessage(List[1]); //bbb=2//可以通过 Values 赋值:List.Values['bbb'] := '22';ShowMessage(List[1]); //bbb=22List.Free;end;//避免重复值varList: TStringList;beginList := TStringList.Create;List.Add('aaa');List.Sorted := True; //需要先指定排序List.Duplicates := dupIgnore; //如有重复值则放弃List.Add('aaa');ShowMessage(List.Text); //aaa//Duplicates 有3个可选值://dupIgnore: 放弃;//dupAccept: 结束;//dupError: 提示错误.List.Free;end;//排序与倒排序{排序函数}functionDescCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;beginResult := -AnsiCompareText(List[Index1], List[Index2]);end;procedureTForm1.Button1Click(Sender: TObject);varList: TStringList;beginList := TStringList.Create;List.Add('bbb');List.Add('ccc');List.Add('aaa');//未排序ShowMessage(List.Text); //bbb ccc aaa//排序List.Sort;ShowMessage(List.Text); //aaa bbb ccc//倒排序List.CustomSort(DescCompareStrings); //调用排序函数ShowMessage(List.Text); //ccc bbb aaa//假如:List.Sorted := True;List.Add('999');List.Add('000');List.Add('zzz');ShowMessage(List.Text); //000 999 aaa bbb ccc zzzend;//高级应用 Delphi中stringlist分割字符串的用法TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的。 常规的用法大家都知道,现在来讨论它的一些高级的用法。 1、CommaText 2、Delimiter &DelimitedText 3、Names &Values &ValueFromIndex 先看第一个:CommaText。怎么用呢? const constr :String = 'aaa,bbb,ccc,ddd'; var strs :TStrings; i :Integer; begin strs := TStringList.Create; strs.CommaText := constr; for i := 0 to Strs.Count-1 do ShowMessage(Strs[i]); end; 执行了这段代码后,可以看到ShowMessage显示出来的分别是:aaa bbb ccc ddd。 也就是说,strs.CommaText := constr这一句的作用, 就是把一个字符串以','为分割符,分段添加到TStrings中。 那么如果不是以','来分割,又该怎么做呢?现在看第二个例子。使用Delimiter和DelimitedText。 const constr :String = 'aaa\bbb\ccc\ddd'; var strs :TStrings; i :Integer; begin strs := TStringList.Create; strs.Delimiter := '\'; strs.DelimitedText := constr; for i := 0 to Strs.Count-1 do ShowMessage(Strs[i]); end; 可以看到, 显示的效果和第一个例子是一模一样的。解释一下: Delimiter为分隔符,默认为:','。DelimitedText就是按Delimiter为分隔符的一个串, 得到赋值后回把这个字符串按Delimiter的字符添加到TStrings中。 说到这里,有想起一个属性,QuoteChar。其默认值为:'"'(不包括单引号) 有何用呢?看例子: const constr :String = '"aaa"\"bbb"\"ccc"\"ddd"'; var strs :TStrings; i :Integer; begin strs := TStringList.Create; strs.Delimiter := '\'; strs.DelimitedText := constr; for i := 0 to Strs.Count-1 do ShowMessage(Strs[i]); end; 显示出来的仍然是aaa bbb ccc ddd。为什么不是:"aaa" "bbb" "ccc" "ddd"呢? 再来看一个例子: const constr :String = '|aaa|\|bbb|\|ccc|\|ddd|'; var strs :TStrings; i :Integer; begin strs := TStringList.Create; strs.Delimiter := '\'; strs.QuoteChar := '|'; strs.DelimitedText := constr; for i := 0 to Strs.Count-1 do ShowMessage(Strs[i]); end; 显示出来的又是aaa bbb ccc ddd。对比一下,应该不难明白吧?这个就不多说了,用得也不多。 但是还要多说一句,当Delimiter为:','而QuoteChar为:'"'时, DelimitedText和CommaText是同等的。 最后要说的三个是:Names &Values &ValueFromIndex。 看看下面的代码: const constr :String = '0=aaa,1=bbb,2=ccc,3=ddd'; var strs :TStrings; i :Integer; begin strs := TStringList.Create; strs.CommaText := constr; for i := 0 to strs.Count-1 do begin ShowMessage(strs.Names[i]); ShowMessage(strs.Values[strs.Names[i]]); ShowMessage(strs.ValueFromIndex[i]); end; end; 通过这个例子不难看出: 这个时候strs中的内容是: 0=aaa 1=bbb 2=ccc 3=ddd 而Names中则是: 0 1 2 3 在Values中则是: aaa bbb ccc ddd 赞 收藏 评论 分享 举报上一篇:delphi中WEBBrowser网页JS函数调用delphi函数 下一篇:将String转化为TStringList |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |